const db = uniCloud.database()
const collection = db.collection('topicList')

exports.main = async (event, context) => {
	try {
		if(!event.topicClass) delete event['topicClass']
		const result = await collection.where(event).get()
		
		console.log(result , 'result');

		let oldArray = JSON.parse(JSON.stringify(result.data))
		// 准备一个新的容器
		let newArray = []

		result.data.forEach(item => {
			
			if(event.topicClass){
				if(newArray.length >= 30){
					return
				}
			}else {
				if(newArray.length >= 60){
					return
				}
			}
			
			// 获取一个随机数
			let random = Math.floor(Math.random() * oldArray.length)
			newArray.push(oldArray[random])
			// 删除当前随机数得数据
			oldArray.splice(random, 1)
		})

		console.log(result, 'result');

		return {
			code: 200,
			data: newArray
		}
	} catch (e) {
		return {
			code: 500,
			message: e
		}
	}
}